package com.ruoyi.business.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.business.domain.BusinessMessage;
import com.ruoyi.business.domain.BusinessReminder;
import com.ruoyi.business.mapper.BusinessMessageMapper;
import com.ruoyi.business.mapper.BusinessReminderMapper;
import com.ruoyi.business.service.IBusinessMessageService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;

/**
 * 消息管理 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class BusinessMessageServiceImpl implements IBusinessMessageService 
{
    @Autowired
    private BusinessMessageMapper businessMessageMapper;
    
    @Autowired
    private BusinessReminderMapper businessReminderMapper;

    /**
     * 查询消息信息
     * 
     * @param messageId 消息ID
     * @return 消息信息
     */
    @Override
    public BusinessMessage selectBusinessMessageById(Long messageId)
    {
        return businessMessageMapper.selectBusinessMessageById(messageId);
    }

    /**
     * 查询消息列表
     * 
     * @param businessMessage 消息信息
     * @return 消息集合
     */
    @Override
    public List<BusinessMessage> selectBusinessMessageList(BusinessMessage businessMessage)
    {
        return businessMessageMapper.selectBusinessMessageList(businessMessage);
    }

    /**
     * 新增消息
     * 
     * @param businessMessage 消息信息
     * @return 结果
     */
    @Override
    public int insertBusinessMessage(BusinessMessage businessMessage)
    {
        if (businessMessage.getRetryCount() == null) {
            businessMessage.setRetryCount(0);
        }
        if (businessMessage.getStatus() == null) {
            businessMessage.setStatus("0"); // 默认为待发送
        }
        return businessMessageMapper.insertBusinessMessage(businessMessage);
    }

    /**
     * 修改消息
     * 
     * @param businessMessage 消息信息
     * @return 结果
     */
    @Override
    public int updateBusinessMessage(BusinessMessage businessMessage)
    {
        return businessMessageMapper.updateBusinessMessage(businessMessage);
    }

    /**
     * 删除消息信息
     * 
     * @param messageId 消息ID
     * @return 结果
     */
    @Override
    public int deleteBusinessMessageById(Long messageId)
    {
        return businessMessageMapper.deleteBusinessMessageById(messageId);
    }

    /**
     * 批量删除消息信息
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBusinessMessageByIds(String ids)
    {
        return businessMessageMapper.deleteBusinessMessageByIds(Convert.toStrArray(ids));
    }
    
    /**
     * 根据提醒ID创建消息
     * 
     * @param reminderId 提醒ID
     * @return 结果
     */
    @Override
    @Transactional
    public int createMessageFromReminder(Long reminderId)
    {
        // 查询提醒信息
        BusinessReminder reminder = businessReminderMapper.selectBusinessReminderById(reminderId);
        if (reminder == null) {
            return 0;
        }
        
        // 创建消息
        BusinessMessage message = new BusinessMessage();
        message.setReminderId(reminder.getReminderId());
        message.setMobilePhone(reminder.getMobilePhone());
        message.setUserName(reminder.getUserName());
        message.setContent(reminder.getContent());
        message.setMessageType("1"); // 默认短信消息
        if ("2".equals(reminder.getChannel())) {
            message.setMessageType("2"); // 微信消息
        }
        message.setStatus("0"); // 待发送
        message.setRetryCount(0);
        message.setBatchNo(DateUtils.dateTimeNow()); // 使用当前时间作为批次号
        message.setCreateBy(reminder.getCreateBy());
        
        return businessMessageMapper.insertBusinessMessage(message);
    }
    
    /**
     * 发送消息
     * 
     * @param messageId 消息ID
     * @return 结果
     */
    @Override
    public int sendMessage(Long messageId)
    {
        // 查询消息信息
        BusinessMessage message = businessMessageMapper.selectBusinessMessageById(messageId);
        if (message == null || !"0".equals(message.getStatus())) {
            return 0;
        }
        
        // TODO: 这里实际应该调用短信/微信发送服务
        boolean sendSuccess = true;
        
        // 更新消息状态
        BusinessMessage updateMessage = new BusinessMessage();
        updateMessage.setMessageId(messageId);
        updateMessage.setSendTime(new Date());
        
        if (sendSuccess) {
            updateMessage.setStatus("1"); // 已发送
        } else {
            updateMessage.setStatus("2"); // 发送失败
            updateMessage.setFailReason("发送失败");
            updateMessage.setRetryCount(message.getRetryCount() + 1);
        }
        
        return businessMessageMapper.updateMessageStatus(updateMessage);
    }
    
    /**
     * 批量发送消息
     * 
     * @param messageIds 消息ID数组
     * @return 结果
     */
    @Override
    public int batchSendMessages(String[] messageIds)
    {
        int successCount = 0;
        for (String idStr : messageIds) {
            Long messageId = Long.valueOf(idStr);
            successCount += sendMessage(messageId);
        }
        return successCount;
    }
} 